package com.stylefeng.guns.rest.modular.auth.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.xml.ws.Response;

public class JsonParseObjectUtil {

	/**
	 * @param Url
	 * @param query
	 * @return  通过post提交获得接口
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static JSONObject postMethod(String Url,String query) throws Exception {
		URL url = new URL(Url);
		HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
		 //请求方式
		 openConnection.setRequestMethod("POST");
        //设置是否从httpUrlConnection读入，默认情况下是true; httpUrlConnection.setDoInput(true);
		openConnection.setDoOutput(true);
        //allowUserInteraction 如果为 true，则在允许用户交互（例如弹出一个验证对话框）的上下文中对此 URL 进行检查。
		openConnection.setAllowUserInteraction(false);
        PrintStream ps = new PrintStream(openConnection.getOutputStream());
        ps.print(query);
        ps.close();
		BufferedReader bf = new BufferedReader(new InputStreamReader(openConnection.getInputStream(),"UTF-8"));
		String inputLine = "";
		String json = "";
		while ((inputLine = bf.readLine()) != null) {
			json += inputLine;
		}

		JSONObject jsonObject = JSONObject.parseObject(json);
		//Response<Object> parseObject = JSON.parseObject(json, Response.class);
		return jsonObject;

	}
	
	
	/**
	 * @param Url，通过get方法获得接口
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static Object getMethod(String Url) throws Exception {
		URL url = new URL(Url);
		HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
		BufferedReader bf = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "UTF-8"));
		String inputLine = "";
		String json = "";
		while ((inputLine = bf.readLine()) != null) {
			json += inputLine;
		}
		Response<Object> parseObject = JSON.parseObject(json, Response.class);
		return parseObject;

	 }
	@SuppressWarnings("unchecked")
	public static Map<String,Map<String , Object>> getMethod1(String Url) throws Exception {
		URL url = new URL(Url);
		HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
		BufferedReader bf = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "UTF-8"));
		String inputLine = "";
		String json = "";
		while ((inputLine = bf.readLine()) != null) {
			json += inputLine;
		}
		Map<String,Map<String , Object>>   lst= (Map<String,Map<String, Object>>) JSON.parse(json);
		return lst;

	 }
	

}
